home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / MPW Related / Universal Interfaces / PPCCIncludes / iostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  14.8 KB  |  610 lines  |  [TEXT/MPS ]

  1. /*
  2.  *------------------------------------------------------------------------
  3.  * Copyright:
  4.  *      © 1993 by Apple Computer Inc.  all rights reserved.
  5.  *
  6.  * Project:
  7.  *      PowerPC C++ Streams Library
  8.  *
  9.  * Filename:
  10.  *      iostream.h
  11.  *
  12.  * Created:
  13.  *      (unknown)
  14.  *
  15.  * Modified:
  16.  *      Date     Engineer       Comment
  17.  *      -------- -------------- ------------------------------------------
  18.  *      12/01/93 Rudy Wang      Removed the declaration of "static Iostream_init iostream_init;"
  19.  *                              and placed it inside iostream_init.c so that we don't get multiple
  20.  *                              copies of this static object all over the place.
  21.  *------------------------------------------------------------------------
  22.  */
  23.  
  24. #ifndef IOSTREAMH
  25. #define IOSTREAMH
  26.  
  27. // include string.h - Some inlines use memcpy
  28. #include <string.h>
  29.  
  30. #ifdef EOF
  31. #if EOF!=-1
  32. #undef EOF
  33. #define EOF (-1)
  34. #endif
  35. #else
  36. #define EOF (-1)
  37. #endif
  38.  
  39. #ifndef NULL
  40. #define NULL 0
  41. #endif
  42.  
  43. #define zapeof(c) ((c)&0377)
  44.  
  45. typedef long streampos;
  46. typedef long streamoff;
  47.  
  48. class streambuf;
  49. class ostream;
  50.  
  51.  
  52. // removed from ios.c and placed here to avoid forward reference problem - rjd 921223
  53.  
  54. union ios_user_union
  55. {
  56.     long i_word;
  57.     void *p_word;
  58. };
  59.  
  60.  
  61. class ios
  62. {
  63. public:
  64.     /*
  65.      * Some enums are declared in ios to avoid pollution of global namespace
  66.      */
  67.     enum io_state
  68.     {
  69.         goodbit  = 0,
  70.         eofbit   = 1,
  71.         failbit  = 2,
  72.         badbit   = 4,
  73.         hardfail = 0200
  74.     };
  75.     /*
  76.      * hard fail can be set and reset internally, but not via public function
  77.      */
  78.     enum open_mode
  79.     {
  80.         in        =    1,
  81.         out       =    2,
  82.         ate       =    4,
  83.         app       =  010,
  84.         trunc     =  020,
  85.         nocreate  =  040,
  86.         noreplace = 0100
  87.     };
  88.     enum seek_dir
  89.     {
  90.         beg = 0,
  91.         cur = 1,
  92.         end = 2
  93.     };
  94.     /*
  95.      * flags for controlling format
  96.      */
  97.     enum
  98.     {
  99.         skipws     =     01,        // skip whitespace on input
  100.  
  101.         left       =     02,        // padding location
  102.         right      =     04,
  103.         internal   =    010,
  104.  
  105.         dec        =    020,        // conversion base
  106.         oct        =    040,
  107.         hex        =   0100,
  108.  
  109.         showbase   =   0200,        // modifiers
  110.         showpoint  =   0400,
  111.         uppercase  =  01000,
  112.         showpos    =  02000,
  113.  
  114.         scientific =  04000,        // floating point notation
  115.         fixed      = 010000,
  116.  
  117.         unitbuf    = 020000,        // stuff to control flushing
  118.         stdio      = 040000
  119.     };
  120.     static const long basefield;    // = dec | oct | hex
  121.     static const long adjustfield;  // = left | right | internal
  122.     static const long floatfield;   // = scientific | fixed
  123.  
  124. public:
  125.     ios(streambuf*);
  126.     virtual ~ios();
  127.  
  128.     long flags() const          { return x_flags; }
  129.     long flags(long f);
  130.     long setf(long setbits, long field);
  131.     long setf(long);
  132.     long unsetf(long);
  133.     int width() const           { return x_width; }
  134.     int width(int w)            { int i = x_width; x_width = w; return i; }
  135.     int precision(int);
  136.     int precision() const       { return x_precision; }
  137.     int rdstate() const         { return state; }
  138.     char fill(char);
  139.     char fill() const           { return x_fill; }
  140.     ostream* tie(ostream* s);
  141.     ostream* tie()              { return x_tie; }
  142.     operator void*()
  143.     {
  144.         if (state & (failbit|badbit|hardfail))
  145.             return 0;
  146.         else
  147.             return this;
  148.     }
  149.     operator const void*() const
  150.     {
  151.         if (state & (failbit|badbit|hardfail))
  152.             return 0;
  153.         else
  154.             return this;
  155.     }
  156.     int operator!() const       { return state & (failbit|badbit|hardfail); }
  157.     int eof() const             { return state&eofbit; }
  158.     int fail() const            { return state&(failbit|badbit|hardfail); }
  159.     int bad() const             { return state&badbit; }
  160.     int good() const            { return state==0; }
  161.     void clear(int i =0)
  162.     {
  163.         state =  (i&0377) | (state&hardfail);
  164.         ispecial = (ispecial&~0377) | state;
  165.         ospecial = (ospecial&~0377) | state;
  166.     }
  167.     streambuf* rdbuf()          { return bp; }
  168.  
  169. public:
  170.     /*
  171.      * Members related to user allocated bits and words
  172.      */
  173.     long& iword(int);
  174.     void*& pword(int);
  175.     static long bitalloc();
  176.     static int xalloc();
  177.  
  178. private:
  179.     static long nextbit;
  180.     static long nextword;
  181.  
  182.     int nuser;
  183.     union ios_user_union* x_user;
  184.     void uresize(int);
  185.  
  186. public:
  187.     /*
  188.      * static member functions
  189.      */
  190.     static void sync_with_stdio();
  191.  
  192. protected:
  193.     enum
  194.     {
  195.         skipping = 01000,
  196.         tied = 02000
  197.     };
  198.     /*** bits 0377 are reserved for userbits ***/
  199.     streambuf* bp;
  200.     void setstate(int b)
  201.     {
  202.         state |= (b&0377);
  203.         ispecial |= b&~skipping;
  204.         ispecial |= b;
  205.     }
  206.     int         state;  
  207.     int         ispecial;                  
  208.     int         ospecial;
  209.     int         isfx_special;
  210.     int         osfx_special;           
  211.     int         delbuf;
  212.     ostream*    x_tie;
  213.     long        x_flags;
  214.     short       x_precision;
  215.     char        x_fill;
  216.     short       x_width;
  217.  
  218.     static void (*stdioflush)();
  219.  
  220.     void init(streambuf*);
  221.     /*
  222.      * Does the real work of a constructor
  223.      */
  224.     ios();                      // No initialization at all. Needed by multiple inheritance versions
  225.     int assign_private;         // needed by with_assgn classes
  226.  
  227. private:                
  228.     ios(ios&);                  // Declared but not defined
  229.     void operator=(ios&);       // Declared but not defined
  230.  
  231. public:
  232.     /*
  233.      * old stream package compatibility
  234.      */
  235.     int skip(int i);
  236. };
  237.  
  238.  
  239. class streambuf
  240. {
  241. protected:
  242.     short       alloc;  
  243.  
  244. private:
  245.     short       x_unbuf;
  246.     char*       x_base; 
  247.     char*       x_pbase;
  248.     char*       x_pptr; 
  249.     char*       x_epptr;
  250.     char*       x_gptr;
  251.     char*       x_egptr;
  252.     char*       x_eback;
  253.     int         x_blen; 
  254.  
  255. private:
  256.     streambuf(streambuf&);      // Declared but not defined
  257.     void operator=(streambuf&); // Declared but not defined
  258.  
  259. public:
  260.     void dbp();
  261.  
  262. protected:
  263.     char*       base()          { return x_base; }
  264.     char*       pbase()         { return x_pbase; }
  265.     char*       pptr()          { return x_pptr; }
  266.     char*       epptr()         { return x_epptr; }
  267.     char*       gptr()          { return x_gptr; }
  268.     char*       egptr()         { return x_egptr; }
  269.     char*       eback()         { return x_eback; }
  270.     char*       ebuf()          { return x_base+x_blen; }
  271.     int         blen() const    { return x_blen; }
  272.     void setp(char* p, char* ep)
  273.     {
  274.         x_pbase = x_pptr = p; x_epptr = ep;
  275.     }
  276.     void setg(char* eb, char* g, char* eg)
  277.     {
  278.         x_eback = eb; x_gptr = g; x_egptr = eg;
  279.     }
  280.     void pbump(int n)
  281.     {
  282.         x_pptr += n;
  283.     }
  284.     void gbump(int n) 
  285.     {
  286.         x_gptr += n;
  287.     }
  288.     void setb(char* b, char* eb, int a = 0)
  289.     {
  290.         if (alloc && x_base)
  291.             delete x_base;
  292.         x_base = b;
  293.         x_blen = (eb > b) ? (eb - b) : 0;
  294.         alloc = a;
  295.     }
  296.     int unbuffered() const      { return x_unbuf; }
  297.     void unbuffered(int unb)    { x_unbuf = (unb!=0) ; }
  298.     int allocate()
  299.     {
  300.         if (x_base == 0 && !unbuffered())
  301.             return doallocate();
  302.         else
  303.             return 0;
  304.     }
  305.     virtual int doallocate();
  306.  
  307. public : 
  308.     virtual int overflow(int c = EOF);
  309.     virtual int underflow();
  310.     virtual int pbackfail(int c);
  311.     virtual int sync();
  312.     virtual streampos seekoff(streamoff, ios::seek_dir, int = ios::in|ios::out);
  313.     virtual streampos seekpos(streampos, int =ios::in|ios::out);
  314.     virtual int xsputn(const char*  s,int n);
  315.     virtual int xsgetn(char*  s,int n);
  316.  
  317.     int in_avail()
  318.     {
  319.         return x_gptr < x_egptr ? x_egptr-x_gptr : 0;
  320.     }
  321.     int out_waiting()
  322.     {
  323.         if (x_pptr)
  324.             return x_pptr-x_pbase;
  325.         else
  326.             return 0; 
  327.     }
  328.  
  329.     int sgetc()
  330.     {
  331.         /*
  332.          * WARNING: sgetc does not bump the pointer
  333.          */
  334.         return (x_gptr >= x_egptr) ? underflow() : zapeof(*x_gptr);
  335.     }
  336.     int snextc()
  337.     {
  338.         return (++x_gptr >= x_egptr) ? x_snextc() : zapeof(*x_gptr);
  339.     }
  340.     int sbumpc()
  341.     {
  342.         return (x_gptr >= x_egptr && underflow() == EOF) ? EOF : zapeof(*x_gptr++);
  343.     }
  344.     int optim_in_avail()
  345.     {
  346.         return x_gptr < x_egptr;
  347.     }
  348.     int optim_sbumpc()
  349.     {
  350.         return zapeof(*x_gptr++); 
  351.     }
  352.     void stossc()
  353.     {
  354.         if (x_gptr >= x_egptr)
  355.             underflow();
  356.         else
  357.             x_gptr++;
  358.     }
  359.     int sputbackc(char c)
  360.     {
  361.         if (x_gptr > x_eback)
  362.         {
  363.             if (*--x_gptr == c)
  364.                 return zapeof(c);
  365.             else
  366.                 return zapeof(*x_gptr=c);
  367.         }
  368.         else
  369.             return pbackfail(c);
  370.     }
  371.     int sputc(int c)
  372.     {
  373.         return (x_pptr >= x_epptr) ? overflow(zapeof(c)) : zapeof(*x_pptr++=c);
  374.     }
  375.     int sputn(const char*  s,int n)
  376.     {
  377.         if (n <= (x_epptr - x_pptr))
  378.         {
  379.             memcpy(x_pptr,s,n);
  380.             pbump(n);
  381.             return n;
  382.         }
  383.         else
  384.             return xsputn(s,n);
  385.     }
  386.     int sgetn(char*  s,int n)
  387.     {
  388.         if (x_gptr + n <= x_egptr)
  389.         {
  390.             memcpy(s,x_gptr,n);
  391.             gbump(n);
  392.             return n;
  393.         }
  394.         else
  395.             return xsgetn(s,n);
  396.     }
  397.     virtual streambuf* setbuf(char* p, int len);
  398.     streambuf* setbuf(unsigned char* p, int len);
  399.     streambuf* setbuf(char* p, int len, int count);     // obsolete third argument
  400.     /*
  401.      * Constructors -- should be protected
  402.      */
  403.     streambuf();
  404.     streambuf(char* p, int l);
  405.     streambuf(char* p, int l, int c);                   // 3 argument form is obsolete. Use strstreambuf.
  406.     virtual ~streambuf();
  407.  
  408. private:
  409.     int x_snextc();
  410. };
  411.  
  412. class istream : virtual public ios
  413. {
  414. public:
  415.     /*
  416.      * Constructor, destructor
  417.      */
  418.     istream(streambuf*);
  419.     virtual ~istream();
  420.  
  421. public:
  422.     int         ipfx(int noskipws=0);
  423.     void        isfx()                                  { }
  424.     istream&    seekg(streampos p);
  425.     istream&    seekg(streamoff o, ios::seek_dir d);
  426.     streampos   tellg();
  427.     istream&    operator>>(istream& (*f)(istream&))     { return (*f)(*this); }
  428.     istream&    operator>>(ios& (*f)(ios&));
  429.     istream&    operator>>(char*);
  430.     istream&    operator>>(unsigned char*);
  431.     istream&    operator>>(unsigned char& c);
  432.     istream&    operator>>(char& c);
  433.     istream&    operator>>(short&);
  434.     istream&    operator>>(int&);
  435.     istream&    operator>>(long&);
  436.     istream&    operator>>(unsigned short&);
  437.     istream&    operator>>(unsigned int&);
  438.     istream&    operator>>(unsigned long&);
  439.     istream&    operator>>(float&);
  440. #ifdef MAC
  441.     istream&    operator>>(long double &);
  442. #endif
  443.     istream&    operator>>(double&);
  444.     istream&    operator>>(streambuf*);
  445.     istream&    get(char* , int lim, char delim='\n');
  446.     istream&    get(unsigned char* b,int lim, char delim='\n');
  447.     istream&    getline(char* b, int lim, char delim='\n');
  448.     istream&    getline(unsigned char* b, int lim, char delim='\n');
  449.     istream&    get(streambuf& sb, char delim ='\n');
  450.     istream&    get_complicated(unsigned char& c);
  451.     istream&    get_complicated(char& c);
  452.     istream&    get(unsigned char& c);
  453.     istream&    get(char& c);
  454.     int get()
  455.     {
  456.         int c;
  457.         if (!ipfx(1))
  458.             return EOF;
  459.         else
  460.         {
  461.             c = bp->sbumpc();
  462.             if (c == EOF)
  463.                 setstate(eofbit);
  464.             return c;
  465.         }
  466.     }
  467.     int peek() 
  468.     {
  469.         if (ipfx(-1))
  470.             return bp->sgetc();
  471.         else
  472.             return EOF;
  473.     }
  474.     istream&    ignore(int n=1,int delim=EOF);
  475.     istream&    read(char*  s,int n);
  476.     istream&    read(unsigned char* s,int n)            { return read((char*)s,n); }
  477.     int         gcount();
  478.     istream&    putback(char c);
  479.     int         sync()                                  { return bp->sync(); }
  480.     void        eatwhite();
  481.  
  482. protected:  
  483.     istream();
  484.  
  485. private: 
  486.     int         x_gcount;
  487.     void        xget(char*  c);
  488.     long scan_int();
  489. };
  490.  
  491. class ostream : virtual public ios
  492. {
  493. public:
  494.     /*
  495.      * Constructor, destructor
  496.      */
  497.     ostream(streambuf*);
  498.     virtual ~ostream();
  499.  
  500. public: 
  501.     int         opfx();         /* Output prefix */
  502.     void        osfx();
  503.  
  504.     ostream&    flush();
  505.     ostream&    seekp(streampos p);
  506.     ostream&    seekp(streamoff o, ios::seek_dir d);
  507.     streampos   tellp();
  508.     ostream&    put(char c);
  509.     ostream&    operator<<(char c);
  510.     ostream&    operator<<(unsigned char c);
  511.     ostream&    operator<<(const char*);
  512.     ostream&    operator<<(const unsigned char*);
  513.     ostream&    operator<<(int a); 
  514.     ostream&    operator<<(long);
  515. #ifdef MAC
  516.     ostream&    operator<<(long double);
  517. #endif
  518.     ostream&    operator<<(double);
  519.     ostream&    operator<<(float);
  520.     ostream&    operator<<(unsigned int a);
  521.     ostream&    operator<<(unsigned long);
  522.     ostream&    operator<<(void*);
  523.     ostream&    operator<<(streambuf*);
  524.     ostream&    operator<<(short i)                     { return *this << (int)i; }
  525.     ostream&    operator<<(unsigned short i)            { return *this << (int)i; }
  526.     ostream&    operator<< (ostream& (*f)(ostream&))    { return (*f)(*this); }
  527.     ostream&    operator<< (ios& (*f)(ios&));
  528.     ostream&    write(const char*  s,int n)     
  529.     {
  530.         if (!state)
  531.         {
  532.             if (bp->sputn(s, n) != n)
  533.                 setstate(eofbit|failbit);
  534.         }
  535.         return *this;
  536.     }
  537.     ostream&    write(const unsigned char* s, int n)    { return write((const char*)s, n); }
  538.  
  539. protected:
  540.     int         do_opfx();
  541.     void        do_osfx();
  542.     ostream();
  543. };
  544.  
  545. class iostream : public istream, public ostream
  546. {
  547. public:
  548.     iostream(streambuf*);
  549.     virtual ~iostream();
  550.  
  551. protected:
  552.     iostream();
  553. };
  554.  
  555. class istream_withassign : public istream
  556. {
  557. public:
  558.     istream_withassign();
  559.     virtual ~istream_withassign();
  560.     istream_withassign& operator=(istream&);
  561.     istream_withassign& operator=(streambuf*);
  562. };
  563.  
  564. class ostream_withassign : public ostream
  565. {
  566. public:
  567.     ostream_withassign();
  568.     virtual ~ostream_withassign();
  569.     ostream_withassign& operator=(ostream&);
  570.     ostream_withassign& operator=(streambuf*);
  571. };
  572.  
  573. class iostream_withassign : public iostream
  574. {
  575. public:
  576.     iostream_withassign();
  577.     virtual ~iostream_withassign();
  578.     iostream_withassign& operator=(ios&);
  579.     iostream_withassign& operator=(streambuf*);
  580. };
  581.  
  582. extern istream_withassign cin;
  583. extern ostream_withassign cout;
  584. extern ostream_withassign cerr;
  585. extern ostream_withassign clog;
  586.  
  587. ios&            dec(ios&);
  588. ostream&        endl(ostream& i);
  589. ostream&        ends(ostream& i);
  590. ostream&        flush(ostream&);
  591. ios&            hex(ios&);
  592. ios&            oct(ios&); 
  593. istream&        ws(istream&);
  594.  
  595.  
  596. /*
  597.  * see iostream_init.c (or MPW cstearms.c)
  598.  */
  599. class Iostream_init
  600. {
  601.     static int stdstatus;
  602.     static int initcount;
  603.     friend ios;
  604. public:
  605.     Iostream_init();
  606.     ~Iostream_init();
  607. };
  608.  
  609. #endif
  610.